#include "comm.h"
#include "config.h"

void Comm_Ack(u8 cmd, u8 size, u8 *buf)
{
	COMM_FRAME_def comm;
	u16 crc16;
	u8 i;
	comm.head = HEAD_Dev;
	comm.cmd = cmd;
	comm.dsize = size;
	for(i=0;i<size;i++)
		comm.data[i] = buf[i];
	crc16 = CRC16_Get8((u8 *)&comm, size+3);
	comm.data[size] = crc16&0xff;
	comm.data[size+1] = crc16>>8;
	U1_SendBuf((u8 *)&comm, size+5);
}

void Comm_Ack_NoInt(u8 cmd, u8 size, u8 *buf)
{
	COMM_FRAME_def comm;
	u16 crc16;
	u8 i;
	comm.head = HEAD_Dev;
	comm.cmd = cmd;
	comm.dsize = size;
	for(i=0;i<size;i++)
		comm.data[i] = buf[i];
	crc16 = CRC16_Get8((u8 *)&comm, size+3);
	comm.data[size] = crc16&0xff;
	comm.data[size+1] = crc16>>8;
	Debug_Sendbuf((u8 *)&comm, size+5);
}


void Comm_Receive(u8 *buf, u8 count)
{
	u16 crc16,crc;
    COMM_FRAME_def *ptr;
    ptr = (COMM_FRAME_def *)buf;
    if(ptr->dsize != (count-5))
    	    return;
    crc16 = buf[count-1]*256 + buf[count-2];
    crc = CRC16_Get8(buf, count-2);
    if(crc != crc16)
    	    return;
    switch(ptr->head)
    {
        case HEAD_BL:
        	BL_Receive(ptr->cmd, ptr->dsize, ptr->data);
            break;
    }
}


